package com.itheima.service.impl;

import com.itheima.dao.UserDao;
import com.itheima.service.UserService;
import com.itheima.utils.MyConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;


/**
 * service层主要做的是处理业务逻辑
 *
 *
 * 注入UserDao
 * 注入获取连接的工具类
 *
 * 注解Service
 *
 * 1  首先获取一个连接  (同时开启事务并关闭事务的自动提交)
 *
 * 2  执行userDao的out方法
 *
 * 3  模拟死机
 *
 * 4  执行userDao的in方法
 *
 * 5  提交和关闭事务
 *
 * 6  处理异常  如果出现异常  那么就进行回滚
 *
 */

@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserDao userDao;

    @Autowired
    private MyConnection myConnection;

    @Override
    public void tranfer(String outName, String inName, int money) throws SQLException {

        try {
            System.out.println("转账开始执行了");

            myConnection.getConnection();

            userDao.out(outName,money);

            //int i = 1/0;

            userDao.in(inName,money);

            myConnection.commitAndClose();

        } catch (SQLException e) {
            e.printStackTrace();
            myConnection.rollbackAndClose();
        }

    }
}
